<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否热门推荐" prop="isRm">
        <el-select v-model="queryParams.isRm" placeholder="请选择是否热门推荐" clearable>
          <el-option
            v-for="dict in dict.type.is_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否上架" prop="isSj">
        <el-select v-model="queryParams.isSj" placeholder="请选择是否上架" clearable>
          <el-option
            v-for="dict in dict.type.is_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否限购" prop="isXg">
        <el-select v-model="queryParams.isXg" placeholder="请选择是否限购" clearable>
          <el-option
            v-for="dict in dict.type.is_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <div   v-if="viewType!='selectpro'">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['business:product:add']"
          >新增</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="plhandleUp"
          >批量上架</el-button>
        </el-col>

        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="plhandleDown"
          >批量下架</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['business:product:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['business:product:remove']"
          >删除</el-button>
        </el-col>


      </div>



      <el-button
        type="danger"
        plain
        icon="el-icon-plus"
        size="mini"
        v-if="viewType=='selectpro'"
        @click="confirmSelect"
      >确定选择</el-button>




      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange" @row-click="singleElection">

      <el-table-column label="序号" width="65" v-if="viewType=='selectpro'">
        <template slot-scope="scope">
          <el-radio
            class="radio"
            v-model="templateSelection"
            :label="scope.$index + 1"
            :value="scope.row.id"
            tabindex="0"
          >
          &nbsp;
          </el-radio>
        </template>
      </el-table-column>

      <el-table-column type="selection"   v-if="viewType!='selectpro'" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="编码" align="center" prop="productNo" />

      <el-table-column label="封面图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="类型 " align="center" prop="typeName" />
      <el-table-column label="简介" align="center" prop="info" />
      <el-table-column :label="spLx =='1'? '价格' : '积分'" align="center" prop="ylPrice" />
      <el-table-column label="是否热门推荐"  v-if="spLx =='1'" align="center" prop="isRm">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_no" :value="scope.row.isRm"/>
        </template>
      </el-table-column>
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="是否上架" align="center" prop="isSj">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_no" :value="scope.row.isSj"/>
        </template>
      </el-table-column>
      <el-table-column label="上架时间" align="center" prop="sjDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sjDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  v-if="viewType!='selectpro'">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="编码" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="分类" prop="type">
          <el-select v-model="form.type" placeholder="请选择分类"      clearable>
            <el-option
              v-for="dict in typeList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="封面图片" prop="image">
          <image-upload v-model="form.image" :limit="1"/>
        </el-form-item>
        <el-form-item label="轮播图片" prop="lbImage">
          <image-upload v-model="form.lbImage"/>
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input-number v-model="form.stock" placeholder="请输入" :min="0" />
        </el-form-item>
        <el-form-item label="是否限购" prop="isXg">
          <el-select     v-model="form.isXg" placeholder="请选择是否限购">
            <el-option
              v-for="dict in dict.type.is_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="限购数量" v-if="form.isXg=='1'" prop="xgNum">
          <el-input-number v-model="form.xgNum" placeholder="请输入" :min="1" />
        </el-form-item>
        <el-form-item label="限购天数"  v-if="form.isXg=='1'"  prop="xgDay">
          <el-input-number v-model="form.xgDay" placeholder="请输入" :min="1" />
        </el-form-item>
        <el-form-item label="简介" prop="info">
          <el-input v-model="form.info" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="详细描述">
          <editor v-model="form.detail" :min-height="192"/>
        </el-form-item>
        <el-form-item label="使用说明">
          <editor v-model="form.sysm" :min-height="192"/>
        </el-form-item>
        <el-form-item :label="spLx =='1'? '价格' : '积分'" prop="ylPrice">
          <el-input-number v-model="form.ylPrice" placeholder="请输入" :min="0.1" />
        </el-form-item>
        <el-form-item v-if="spLx =='1'"  label="是否热门推荐" prop="isRm">
          <el-select     v-model="form.isRm" placeholder="请选择是否热门推荐">
            <el-option
              v-for="dict in dict.type.is_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  batchOnShelf,
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
  batchOffShelf
} from '@/api/business/product'
import { alllistType } from '@/api/business/producttype'

export default {
  name: "Product",
  props: {
      viewType: {
        type: String,
        default: null
      },
    spLx:{
      type: String,
      default: null
    },
  },
  dicts: ['is_no'],
  data() {
    return {
      templateSelection:null,
      selectRow:null,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        image: null,
        lbImage: null,
        type: null,
        info: null,
        detail: null,
        sysm: null,
        ylPrice: null,
        yhPrice: null,
        isRm: null,
        isSj: null,
        sjDate: null,
        xjDate: null,
        spLx: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "分类不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "封面图片不能为空", trigger: "blur" }
        ],
        lbImage: [
          { required: true, message: "轮播图片不能为空", trigger: "blur" }
        ],
        info: [
          { required: true, message: "简介不能为空", trigger: "blur" }
        ],
        detail: [
          { required: true, message: "详细描述不能为空", trigger: "blur" }
        ],
        sysm: [
          { required: true, message: "使用说明不能为空", trigger: "blur" }
        ],
        ylPrice: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        isRm: [
          { required: true, message: "是否热门推荐不能为空", trigger: "change" }
        ],
      },
      multipleSelection:[],
      typeList:[]
    };
  },
  created() {
    this.getList();
    // 获取所有分类
    this.getAlPtype();
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.spLx = this.spLx;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAlPtype(){
      alllistType().then(response => {
        this.typeList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        image: null,
        lbImage: null,
        type: null,
        info: null,
        detail: null,
        sysm: null,
        ylPrice: null,
        yhPrice: null,
        isRm: null,
        isSj: null,
        sjDate: null,
        xjDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        spLx: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    singleElection (row) {
      this.selectRow=row
      this.templateSelection = this.productList.indexOf(row);
      this.ids= row.id;
    },

    confirmSelect() {
      if(!this.selectRow){
        this.$modal.msgError("请选择一条记录");
        return
      }
      // 传递个父组件
      this.$emit('selectProduct',this.selectRow)

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息";
      this.form.spLx = this.spLx;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品信息";
      });
    },
    plhandleUp(){
      // 批量上架
      if(this.ids.length>0){
        const ids=this.ids
        this.$modal.confirm('是否确认上架商品信息编号为"' + this.ids + '"的数据项？').then(function() {
          console.log(ids+"8888888888888888888888888888");
          return batchOnShelf(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("上架成功");
        }).catch(() => {});
      }else{
        console.log("请选择要上架的商品信息");
        this.$modal.msgError("请选择要上架的商品信息");
      }
    },
    plhandleDown(){
      // 下架
      if(this.ids.length>0){
        const ids=this.ids
        this.$modal.confirm('是否确认下架商品信息编号为"' + this.ids + '"的数据项？').then(function() {
          return batchOffShelf(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("下架成功");
        }).catch(() => {});
      }else{
        console.log("请选择要下架的商品信息");
        this.$modal.msgError("请选择要下架的商品信息");
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品信息编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
